 
# -*- coding: utf-8 -*-
import os
import time

from prologixGpibEthernet import GPIB
from config.preferences import SRC_PATH, IPADDR


class VNA8753E(GPIB):

    def __init__(self, ui):
        """
            Construtor da classe.
        """
        self.ui = ui
        GPIB.__init__(self, ui)



    def _connect(self):
        """
            Metodo interno para conexao do network analyzer.
        """
        self.connect(IPADDR)
        self.select(16)
        self.write("*CLS;")



    def startUpConfig(self):
        """
            Metodo que executa as configuracoes iniciais para o VNA.
        """
        self._connect()
        self.write("talklist")
        self.write("pltprthpib")
        self.closeConnection()


    def plotLabel(self, produto, numAntena):
        """
        
        """
        self._connect()
        num_serie = ("%s-%s") % (numAntena[0], numAntena[1])
        line = ('TITL "%s - Serial:%s"') % (produto, num_serie)
        self.write(line)
        self.flush()
        self.write(line)
        self.flush()
        self.closeConnection()



    def getPlot(self):
        """
            Metodo para a transferencia do plot para o computador.
        """
        self._connect()
        self.ui.progressBar.setValue(100)
        self.write("OUTPPLOT")
        data = self.read()
        os.chdir(SRC_PATH + '/tmp')
        plot = open("plot.hp", "w")
        plot.write(data)
        plot.close()
        os.system(
        "hp2xx -q -w200 -h200 -c44221422 -p11111111 -m jpg plot.hp --outfile plot.jpg")
        os.chdir(SRC_PATH)
        self.closeConnection()
